package com.lily.aidouzixun;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lily.aidouzixun.adapter.ViewPagerAdapter;
import com.lily.aidouzixun.activity.MainActivity;

/**
 * 引导页面，只引导一次
 */
public class GuideActivity extends FragmentActivity implements View.OnClickListener,OnPageChangeListener {
    private static final String TAG = "GuideActivity";

    //ViewPage相关属性
    private ViewPager mViewPager;
    private ViewPagerAdapter mViewPageAdapter;
    private LinearLayout llDot;
    private SparseArray<View> mViews;
    private static final int[] pics = {R.drawable.g1,
            R.drawable.g2, R.drawable.g3,
            R.drawable.g4, };
    private ImageView[] dots;   //小点
    private int curIndex;   //当前位置
    private boolean isLastViewPage; //ViewPage是否到达最后一页
    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.guide);
        init();
        findViews();
        initDots();
    }

    /**
     * 初始化参数
     */
    private void init() {
        mContext = GuideActivity.this;
        mViews = new SparseArray<View>();
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
        for(int i = 0; i <  pics.length; i++) {
            View imageviewView = LayoutInflater.from(mContext).inflate(R.layout.imageview, null);
            ImageView imageView = (ImageView) imageviewView.findViewById(R.id.imageview);
            if (i == pics.length - 1) {
                imageView.setOnClickListener(new View.OnClickListener() {   //最后一张图片
                    @Override
                    public void onClick(View view) {
                        goHome();
                    }
                });
            }

            imageView.setLayoutParams(mParams);
            imageView.setImageResource(pics[i]);
            mViews.put(i, imageView);
        }
        curIndex = 0;
    }

    /**
     * 得到控件
     */
    private void findViews() {
        mViewPager = (ViewPager)findViewById(R.id.startup_viewpager);
        mViewPageAdapter = new ViewPagerAdapter(mViews);
        mViewPager.setAdapter(mViewPageAdapter);
        mViewPager.setOnPageChangeListener(this);
    }

    /**
     * 初始化小点
     */
    private void initDots() {
        llDot = (LinearLayout)findViewById(R.id.dot_ll);
        dots = new ImageView[pics.length];
        for(int i = 0; i < pics.length; i++) {
            dots[i] = (ImageView)llDot.getChildAt(i);
            dots[i].setEnabled(true);
            dots[i].setOnClickListener(this);
            dots[i].setTag(i);
        }
        dots[curIndex].setEnabled(false);
    }
    /**
     * 跳转到主页页面
     */
    private void goHome() {
        Intent intent = new Intent(GuideActivity.this, MainActivity.class);
        startActivity(intent);
        finish();   //回退，不让进引导页面
    }

    /**
     * 设置当前的引导页
     * @param position
     */
    private void setCurView(int position) {
        if(position < 0 || position > pics.length) {
            return;
        }
        mViewPager.setCurrentItem(position);
    }

    /**
     * 设置当前页小点
     * @param position
     */
    private void setCurDot(int position) {
        if(position <0 || position > pics.length - 1 || position == curIndex) {
            return;
        }
        dots[position].setEnabled(false);
        dots[curIndex].setEnabled(true);

        curIndex = position;
    }

    /**
     * 对小点的监听
     * @param v
     */
    @Override
    public void onClick(View v) {
        int position = (Integer) v.getTag();
        setCurDot(position);
        setCurView(position);
    }

    /**
     * 页面滚动时调用
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    /**
     * 页面被选择时调用
     * @param position
     */
    @Override
    public void onPageSelected(int position) {
        //滑动时，改变点的状态
        if (position == mViewPager.getAdapter().getCount() - 1) {
            llDot.setVisibility(View.GONE);
        } else {
            llDot.setVisibility(View.VISIBLE);
        }

        setCurDot(position);
    }

    /**
     * 页面滑动状态改变时调用
     * @param state
     */
    @Override
    public void onPageScrollStateChanged(int state) {

        switch (state) {
            case ViewPager.SCROLL_STATE_DRAGGING:
                isLastViewPage= false;
                break;
            case ViewPager.SCROLL_STATE_SETTLING:
                isLastViewPage = true;
                break;
            case ViewPager.SCROLL_STATE_IDLE:
                if (mViewPager.getCurrentItem() == mViewPager.getAdapter()
                        .getCount() - 1 && !isLastViewPage) {
                    //滑动到最后一页，跳转到主页面
                    goHome();
                }
                isLastViewPage = true;
                break;
        }
    }
}
